<template>
	<view class="box">
		<view style="padding: 10px; color: #A1A1A1; font-size: 14px;">
			说明：对所选高电平或低电平进行设置，未选择项不进行设置。
		</view>
		<view class="uni-body">
			<view class="uni-list-cell-db" v-for="(item,index) in chargeCabintList" :key="index">
				<picker @change="tempChange($event, index)" :value="item.value" :range="lockData" range-key="name">
					<view class="uni-input">仓{{item.chargingCabinetBinIndex}}：
						<text v-if="item.value&&item.value!='null'">{{lockFilter[item.value]}}</text>
						<text v-else style="color: #999999;">请选择</text>
					</view>
				</picker>
			</view>
		</view>

		<view style="padding: 30px 0px 0px 0px; margin: 40rpx 6%; text-align: center;">
			<button :loading="editLoading" @click="isConfirm()" type="primary" size="mini"
				style="margin-right: 20rpx; background-color: #4DA7FF; width: 47%; height: 80rpx; line-height: 80rpx;">保存</button>
			<button @click="back()" type="primary" size="mini"
				style="background-color: #C2C2C2; width: 47%; height: 80rpx; line-height: 80rpx;">取消</button>
		</view>
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
		
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="success" title="提示" content="确认要设置已选参数吗？"
				@confirm="save()"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		listParameterSending,
		equip,
	} from "@/api/equip.js";
	import {
		overPowerCode,
		normallyOpenCode,
		totalPowerCode,
		lockData
	} from '@/common/interface.js';
	export default {
		data() {
			return {
				form: {
					name: 'test',
					powerLimit: '',
					overPowerThreshold: '',
				},
				optionBox: {},
				editLoading: false,
				msgType: '',
				messageText: '',
				equipid: '', //充电柜id
				code: '', //充电柜code

				changeIndex: '',
				lockData: [ //锁常开常闭
					{
						value: 'null',
						name: "请选择"
					},
					{
						value: "1",
						name: "高电平",
					},
					{
						value: "0",
						name: "低电平",
					},
				],
				chargeCabintList: [],
				lockFilter: {
					'1': '高电平',
					'0': '低电平'
				},
			}
		},

		onLoad(option) {
			this.equipid = option.equipid;
			this.code = option.code;
			this.optionBox = option;
			this.getDetail();
		},
		methods: {
			tempChange(e, index) {
				this.chargeCabintList[index].value = this.lockData[e.detail.value].value;
			},

			// 获取详情
			getDetail() {
				equip('/selectParams', 'get', {
						code: this.code,
						overPowerCode: overPowerCode,
						totalPowerCode: totalPowerCode,
						normallyOpenCode: normallyOpenCode
					}).then(res => {})
					.catch(err => {
						uni.hideLoading();
						err.forEach((item) => {
							if ('06040101' == item.protocolSemaphoreCode) {
								this.form.powerLimit = item.value;
							}
							if ('06042301' == item.protocolSemaphoreCode) {
								this.form.overPowerThreshold = item.value;
							}
						})
						this.chargeCabintList = err.filter(
							(item) => item.protocolSemaphoreCode == '06043401'
						);
					})
			},
			isConfirm(){
				let cabints = this.chargeCabintList.filter(item => item.value && item.value != 'null');
				if (cabints.length == 0) {
					this.$Toast.error('请选择设置项！');
					return;
				}
				this.$refs.alertDialog.open();
			},
			// 保存
			save() {
				this.$Toast.loading('设置中···');

				let cabints = this.chargeCabintList.filter(item => item.value && item.value != 'null');
				
				let filters = cabints.map((item) => {
					return {
						protocolSemaphoreCode: item.protocolSemaphoreCode,
						value: item.value,
						chargingCabinetBinIndex: item.chargingCabinetBinIndex,
					};
				});
				
				let datas = {
					chargingCabinetId: this.equipid,
					params: filters,
				};

				listParameterSending('/write', 'post', datas)
					.then((res) => {
						if (res.code == 200) {
							this.$Toast.success('参数设置成功');
							setTimeout(() => {
								this.back();
							}, 1500)
						}
					})
			},
			// 返回
			back() {
				let pages = getCurrentPages(); // 当前页面
				let page = getCurrentPages().pop(); // 当前页面
				let beforePage = pages[pages.length - 2]; // 上一页
				uni.navigateBack({
					success: function() {
						beforePage.onLoad(page.options); // 执行上一页的onLoad方法
					}
				});
			}
		}
	}
</script>

<style>
	.uni-input-wrapper {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex-wrap: nowrap;
		background-color: #FFFFFF;
	}

	.uni-input {
		flex: 1;
		background-color: #FFFFFF;
		font-size: 32rpx;
	}

	.uni-icon {
		font-family: uniicons;
		font-size: 18px;
		font-weight: normal;
		font-style: normal;
		width: 36px;
		height: 36px;
		line-height: 26px;
		color: #000000;
	}

	.listBtn {
		width: 250rpx;
		height: 60rpx;
		line-height: 60rpx;
		float: left;
		text-align: center;
		color: #FFFFFF;
		background-color: #4DA7FF;
		border-radius: 5px;
	}

	/deep/ button::after {
		border: none;
	}

	.uni-body {
		background: #FFFFFF;
		padding: 30rpx 60rpx;
	}
</style>
